package com.niit.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.mall.common.domain.dto.PageDTO;
import com.niit.mall.common.utils.BeanUtils;
import com.niit.mall.user.domain.po.Role;
import com.niit.mall.user.domain.po.UserRole;
import com.niit.mall.user.domain.query.RoleQuery;
import com.niit.mall.user.domain.vo.RoleVO;
import com.niit.mall.user.mapper.RoleMapper;
import com.niit.mall.user.mapper.UserRoleMapper;
import com.niit.mall.user.service.IRoleService;
import com.niit.mall.user.service.IUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.user.service.impl
 * @Author: ChuXuan
 * @CreateTime: 2025-06-16  01:39
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    private final IUserRoleService userRoleService;
    private final RoleMapper roleMapper;
    private final UserRoleMapper userRoleMapper;

    @Override
    public RoleVO queryUserRoleByUserId(Long userId) {

        // 1. 根据用户ID查询用户角色信息
        UserRole userRole = userRoleMapper.selectOne(new QueryWrapper<UserRole>().eq("user_id", userId));
        // 3. 根据角色ID查询角色详情
        Role role = baseMapper.selectById(userRole.getRoleId());
        RoleVO roleVO = BeanUtils.copyProperties(role, RoleVO.class);
        roleVO.setId(userRole.getRoleId());
        return roleVO;
    }

    /**
     * 分页查询角色权限列表
     * @param query 查询参数
     * @return 权限分页列表
     */
    @Override
    public PageDTO<RoleVO> queryUserRoleByPage(RoleQuery query) {
        // 获取分页参数
        String name = query.getName();
        Boolean status = query.getStatus();
        String sortBy = query.getSortBy();
        Boolean isAsc = query.getIsAsc();

        // 分页条件
        Page<Role> page = query.toMpPage(sortBy, isAsc);

        // 执行查询
        Page<Role> roleList = lambdaQuery()
                .like(name != null, Role::getName, name)
                .eq(status != null, Role::getStatus, status)
                .page(page);

        // 封装并返回结果
        return PageDTO.of(roleList, RoleVO.class);
    }

    /**
     * 更新角色数量
     * @param roleId 角色ID
     * @param delta 增减数量
     */
   // RoleServiceImpl.java
    @Override
    @Transactional
    public void updateRoleNum(Long roleId, int delta) {
        if (roleId == null || delta == 0) {
            throw new IllegalArgumentException("角色ID不能为空或增量为0");
        }
        lambdaUpdate()
            .eq(Role::getId, roleId)
            .setSql("user_count = user_count + " + delta)
            .set(Role::getUpdateDate, LocalDate.now())
            .update();
    }

    /**
     * 根据角色id获取权限json
     * @param id 角色id
     * @return 权限json
     */
    @Override
    public Map<String, List<String>> queryAssignmentById(Long id) {
        Role role = baseMapper.selectById(id);
        return role.getAssignment();
    }
}
